package com.talent.study.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @Author: hszhou
 * @Date: 2025/4/25 16:02
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
public class Server {

    public static void main(String[] args) throws InterruptedException {

        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup work = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(boss,work)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {

                        ChannelPipeline pipeline = ch.pipeline();
                     //   pipeline.addLast(new LineBasedFrameDecoder(65535));
                        //ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                      //  pipeline.addLast(new DelimiterBasedFrameDecoder(65535, delimiter));
                        pipeline.addLast(new FixedLengthFrameDecoder(20));
                        pipeline.addLast(new ServerHandler());
                    }
                });

        ChannelFuture sync = serverBootstrap.bind(8080).sync();

        sync.channel().closeFuture().addListener(future -> {

            boss.shutdownGracefully();
            work.shutdownGracefully();
        });
    }
}
